package com.szqhyxj.youxiangju.sys.components.view.popview;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.receipt.receiptdetail.signature.SignaturePresenter;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * XiaoPanYu
 * on 2018/10/27
 */


public class GainSMSPopView extends BaseDialog{

    EditText code;
    TextView send;
    TextView cardphone;
    Button cancel;
    Button confirm;
    TextView tiptitle;
    private String title;
    private OnClickListener listener;
    private String resource;
    private static final int MINUTE = 60;

    Boolean check = false;
    public GainSMSPopView(Context context) {
        super(context);
    }


    public GainSMSPopView(Context context, String resource,String tip){
        super(context);
        this.resource = resource;
        cardphone.setText(resource);
        tiptitle.setText(tip);
    }

    @Override
    protected View initDialogView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.popview_gain_sms, null);
        ButterKnife.bind(view);
        code = view.findViewById(R.id.code);
        send = view.findViewById(R.id.send);
        cardphone = view.findViewById(R.id.cardphone);
        cancel = view.findViewById(R.id.cancel);
        confirm = view.findViewById(R.id.confirm);
        tiptitle = view.findViewById(R.id.tiptitle);
        send.setOnClickListener(this);
        cancel.setOnClickListener(this);
        confirm.setOnClickListener(this);
        return view;
    }

    private void startCountDown() {
        Observable
                .interval(0, 1, TimeUnit.SECONDS)
                .take(MINUTE)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) {
                        if (aLong == MINUTE - 1) {
                            send.setText("获取");
                            check = false;
                        } else {
                            send.setText(MINUTE - aLong - 1 + "s");
                        }
                    }
                });
    }

    @OnClick({R.id.send, R.id.cancel, R.id.confirm})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.send:
                check = true;
                if (check){
                    startCountDown();
                    if (listener!=null){
                        listener.onClickgetMsg();
                    }
                }else{

                }

                break;
            case R.id.cancel:
                dismiss();
                break;
            case R.id.confirm:
                if (listener != null) {
                    listener.onClickConfirm(code.getText().toString());
                }

                break;
        }
    }


    public interface OnClickListener {
        void onClickConfirm(String code);

        void onClickgetMsg();
    }

    public void setOnClickListener(OnClickListener listener) {
        this.listener = listener;
    }

    public void setMinute(String min,int code){
        if (code==1){
            send.setText("获取");
            send.setEnabled(true);
        }else {
            send.setText(min);
            send.setEnabled(false);
        }


    }

    public void setTiptitle(String tip){
        tiptitle.setText(tip);
    }



}



